<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>密码策略信息列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/extends/zTree_v3/css/zTreeStyle/zTreeStyle.css"/>
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<body>
<div style="position: relative;height: 100%;" >
    <div class="ccpui-trees" >
        <p class="isShowTree colorSwitch">
            <span style="float: left;color: #fff">&nbsp;&nbsp;分类管理</span>
            <i layadmin-event="treeFlexible" class="layui-icon layui-icon-prev"></i>
        </p>
        <div style="margin-top: 6px;" >
            <ul class="ccpui-iconTree iconTrees">
                <li><a class="iconfont iconshuaxin- zTree-refresh" title="刷新"></a></li>
                <li><a class="iconfont iconwenjianjia1-copy zTree-triangle-d" title="展开"></a></li>
                <li><a class="iconfont iconfolder1 zTree-triangle-r" title="收起"></a></li>
                <li><select style="width: 140px" name="allTypeKey" id="allTypeKey" lay-filter="allTypeKey"></select></li>
            </ul>
            <ul id="treeClass" class="ztree ccpui-listZtree" style="top:65px;"></ul>
        </div>
    </div>
    <div class="ccpui-coreContent" id="addGlobalType" style="background: #fff;overflow: auto;margin-left: 10px;top:10px;bottom: 10px">
        <!--<ul id="dicTree" class="ztree ccpui-listZtree"></ul>-->
    </div>
</div>
<div id="classRightMenus">
    <ul>
        <li id="addNode">增加节点</li>
        <!--<li id="editNode">修改节点</li> -->
        <li id="removeNode">删除节点</li>
    </ul>
</div>
<script src="${ctx}/extends/jquery.min.js" type="text/javascript"></script>
<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script src="${ctx}/extends/zTree_v3/js/jquery.ztree.all.min.js" type="text/javascript"></script>
<script src="${ctx}/extends/zTree_v3/js/jquery.ztree.exhide.min.js" type="text/javascript"></script>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','layer', 'jquery', 'form', 'setter'], function () {
        var layer = layui.layer, setter = layui.setter,form = layui.form,$ = layui.jquery;

        $.ajax({
            url: serverPath.systemPath+setter.sysLinkPath+"globalType/queryAllTypeKey",
            type: "get",
            dataType: 'json',
            contentType: 'application/json',
            success:function (datas) {
                var data = datas.data;
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    if(i == 0){
                        html += '<option selected value="' + data[i].typeKey+ '">' + data[i].typeName + '</option>';
                    }else{
                        html += '<option value="' + data[i].typeKey+ '">' + data[i].typeName + '</option>';
                    }
                }
                $("#allTypeKey").append(html);
                resGlobalType();
            }
        });

        //展开
        $(".zTree-triangle-d").click(function () {
            $.fn.zTree.getZTreeObj("treeClass").expandAll(true);
        });
        //收起
        $(".zTree-triangle-r").click(function () {
            $.fn.zTree.getZTreeObj("treeClass").expandAll(false);
        });
        //刷新
        $(".zTree-refresh").click(function () {
            resGlobalType();
        });

        //下拉选中后的加载
        $("#allTypeKey").change(function () {
            resGlobalType();
        });

        layui.reloadGlobalTypeRes = function () {
            resGlobalType();
        };

        resGlobalType();

        function resGlobalType() {
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + "globalType/list",
                type: "post",
                dataType: 'json',
                data: {"catKey": $("#allTypeKey").val(), "hasRoot": 1},
                success: function (data) {
                    var zNodes = data.data;
                    var setting = {
                        view: {
                            showLine: false,
                            nameIsHTML: true, //允许name支持html
                            selectedMulti: false
                        },
                        edit: {
                            enable: false,
                            editNameSelectAll: false
                        },
                        data: {
                            simpleData: {
                                enable: true,
                                idKey: "typeId",
                                pIdKey: "parentId"

                            },
                            key: {
                                name: "typeName"
                            }
                        },

                        callback: {
                            onClick: onClickType,
                            onRightClick: OnRightClickres
                        }
                    };
                    $.fn.zTree.init($('#treeClass'), setting, zNodes);
                }
            });
        };

        //左键菜单
        function onClickType(event, treeId, treeNode) {
            if (treeNode && !treeNode.noR) {
                selectResId = treeNode.typeId;
                if (treeNode.parentId == null) {
                    return;
                }
                if($("#allTypeKey").val() == "DIC"){
                    $('#addGlobalType').load(serverPath.systemPath + setter.sysLinkPath + "globalType/view/listAddNode?typeId=" + selectResId + "&isAdd=false&isShow=true&isGlobalType=true",function () {
                    });
                }else{
                    $('#addGlobalType').load(serverPath.systemPath + setter.sysLinkPath + "globalType/view/listAddNode?typeId=" + selectResId + "&isGlobalType=true");
                }
            }
        }

        //右键菜单
        function OnRightClickres(event, treeId, treeNode) {
            if (treeNode && !treeNode.noR) {
                ztreeNode = treeNode;
                selectResId = treeNode.typeId;
                parentIds = treeNode.parentId;
                showRMenus(event.clientX, event.clientY);
            }
        }

        function showRMenus(x, y) {
            $("#classRightMenus ul").show();
            var h = $('#classRightMenus').height();
            x += 15;
            y += 10;
            if ($(window).height() < y + h) (
                y = y - h
            )
            $("#classRightMenus").css({"top": y + "px", "left": x + "px", "visibility": "visible"});

            $("body").bind("mousedown", onBodyMouseDown);
        }

        function onBodyMouseDown(event) {
            if (!(event.target.id == "orgRightMenu" || $(event.target).parents("#classRightMenus").length > 0)) {
                $("#classRightMenus").css({"visibility": "hidden"});
            }
        }

        $("#addNode").click(function () {
            var isRoot;
            if (ztreeNode.parentId == null) {
                isRoot = 1;//根节点
            } else {
                isRoot = 0;
            }
            if($("#allTypeKey").val() == "DIC"){
                $('#addGlobalType').load(serverPath.systemPath + setter.sysLinkPath + "globalType/view/listAddNode?isRoot=" + isRoot + "&parentId=" + selectResId + "&isAdd=true&isShow=true&isGlobalType=true",function () {
                    form.render();
                });
            }else{
                $('#addGlobalType').load(serverPath.systemPath + setter.sysLinkPath + "globalType/view/listAddNode?isRoot=" + isRoot + "&parentId=" + selectResId + "&isGlobalType=true");
            }
        });

        $("#removeNode").click(function () {
            if (ztreeNode.parentId == null) {
                layer.alert("根节点不能删除");
            } else {
                layer.confirm('确定删除当前节点吗？', function (index) {
                    layer.close(index);
                    $.ajax({
                        url: serverPath.systemPath + setter.sysLinkPath + 'globalType/del',
                        type: "post",
                        dataType:'json',
                        data: {"typeId": selectResId},
                        success: function (result) {
                        $("#addGlobalType").html("");
                            layer.alert(result.msg);
                            var zTree = $.fn.zTree.getZTreeObj("treeClass");
                            var idArr = selectResId.split(",");
                            $.each(idArr, function (i, value) {
                                var node = zTree.getNodeByParam("typeId", value);
                                zTree.removeNode(node);
                            })
                        }
                    });
                });
            }
        });

    });
</script>
<style>
    .trees .iconTrees li {
        display: inline-block;
        margin: 6px 4px;
        cursor: pointer;
    }
    div#classRightMenus ul li {
        margin: 5px 0;
        padding: 0 8px;
        cursor: pointer;
        list-style: none outside none;

    }
    div#classRightMenus {
        position: fixed;
        border: 1px solid #ccc;
        z-index: 900;
        visibility: hidden;
        top: 0;
        background-color: #f5f5f5;
        text-align: left;
        padding: 2px;
    }
</style>
</body>
</html>